package com.zrrd.yunchmall.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.yunchmall.user.entity.Resource;
import com.zrrd.yunchmall.user.service.IResourceService;
import com.zrrd.yunchmall.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 后台资源表 前端控制器
 * </p>
 *
 * @author LiYe
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/resource")
public class ResourceController {
    @Autowired
    private IResourceService resourceService;
    @GetMapping("/list")
    public ResponseResult list(int pageNum,int pageSize,String nameKeyword,String urlKeyword,Integer categoryId){
        QueryWrapper queryWrapper = new QueryWrapper();
        if (!StringUtils.isEmpty(nameKeyword)){
            queryWrapper.like("name",nameKeyword);
        }
        if (!StringUtils.isEmpty(urlKeyword)){
            queryWrapper.like("url",urlKeyword);
        }
        if (categoryId != null){
            queryWrapper.eq("category_id",categoryId);
        }
        IPage<Resource> pageInfo = resourceService.page(new Page<>(pageNum,pageSize),queryWrapper);
        return new ResponseResult(200,"查询成功",pageInfo);
    }
    @PostMapping("/create")
    public ResponseResult create(@RequestBody Resource resource){
        resourceService.save(resource);
        return new ResponseResult(200,"保存成功");
    }
    @GetMapping("/listAll")
    public ResponseResult listAll(){
        return new ResponseResult(200,"查询成功",resourceService.list());
    }
}
